﻿Option Explicit On
Option Strict On



Public Class AgregarCliente


    Private _Validador As Boolean
    Private ErrorProvider2 As ErrorProvider
    Private _MyBaseDeDAtos As BBDDCita

    Private Enum EstadoBorrar As Integer
        SiBorrar = 1
        NoBorrar = 2
    End Enum
    Private _Borrar As EstadoBorrar ' Controla el borrado de las celdas
    Private ContadorAviso As Integer

    Private Function InformacionYesNo(ByVal texto As String) As Boolean

        Dim ok As Boolean = True
        If MsgBox(texto, MsgBoxStyle.YesNo, "Gestor Cita - Información") = MsgBoxResult.No Then
            ok = False
        End If

        Return ok

    End Function

    Private Function validarDatos() As Boolean
        Dim ok As Boolean = True

        ErrorProvider2.Clear()
        'comprobar si tiene un nombre y un idCliente
        If TbNombre.Text = "" Then
            ErrorProvider2.SetError(TbNombre, "Introduzca Nombre")
            Me.TbNombre.Focus()
            ok = False
        End If

        If Tb1Apellido.Text = "" Then
            Tb1Apellido.Text = " "
        End If

        If Tb2Apellido.Text = "" Then
            Tb2Apellido.Text = " "
        End If

        Try
            If TbTelefono.Text = "" Then
                TbTelefono.Text = " "
            Else
                Dim numero As Integer = CInt(TbTelefono.Text)
            End If
        Catch
            ErrorProvider2.SetError(TbTelefono, "No es un número de Télefono")
            Me.TbNombre.Focus()
            ok = False
        End Try

        If TbDireccion.Text = "" Then
            TbDireccion.Text = " "
        End If

        Try
            If TbCP.Text = "" Then
                TbCP.Text = " "
            Else
                Dim numero As Integer = CInt(TbCP.Text)
            End If
        Catch
            ErrorProvider2.SetError(TbCP, "No es un número")
            Me.TbNombre.Focus()
            ok = False
        End Try

        If TbEmail.Text = "" Then
            TbEmail.Text = " "
        End If
        Return ok
    End Function


    Private Sub Restaurar()
        If _Borrar = EstadoBorrar.SiBorrar Then
            TbNombre.Clear()
            Tb1Apellido.Clear()
            Tb2Apellido.Clear()
            TbCP.Clear()
            TbDireccion.Clear()
            TbEmail.Clear()
            TbTelefono.Clear()
            _Borrar = EstadoBorrar.NoBorrar
        End If
      
        DTPFecha.MaxDate = Now
        ToolStripStatusLabelInformacion.Text = ""
        _Validador = False
    End Sub

    Private Sub Borrar()

        If Me.InformacionYesNo("¿Estás Seguro?") Then
            _Borrar = EstadoBorrar.SiBorrar
            Restaurar()
        End If


    End Sub

    Private Sub AgregarCliente_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ErrorProvider2 = New ErrorProvider
        _MyBaseDeDAtos = New BBDDCita
        _Borrar = EstadoBorrar.SiBorrar
        Restaurar()
        ToolStripStatusLabel1.Text = CStr(Now)
        ToolStripStatusLabelInformacion.Text = ""
        ToolStripStatusLabelInforGuardado.Text = ""
        Timer1.Start()
        ContadorAviso = 0
    End Sub

    Private Sub TimerHora_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ToolStripStatusLabel1.Text = CStr(Now)

        'indica si se a guardado
        If _Validador Then
            ContadorAviso += 1
            If ContadorAviso > 1 Then
                If ToolStripStatusLabelInformacion.Text = "Sin Guardar" Then
                    ToolStripStatusLabelInformacion.Text = ""
                Else
                    ToolStripStatusLabelInformacion.Text = "Sin Guardar"
                End If
                ContadorAviso = 0
            End If
        End If
    End Sub

    Private Sub Guardar()

        If validarDatos() Then
            Dim fecha As String = CStr(DTPFecha.Value)
            If _MyBaseDeDAtos.InsertarCliente(TbNombre.Text, Tb1Apellido.Text, Tb2Apellido.Text, TbTelefono.Text, TbCP.Text, fecha, TbEmail.Text) Then
                MsgBox("Guardado Con exito", , "Información")
                Me.Close()
            Else
                ToolStripStatusLabelInforGuardado.Text = "Guardado Sin exito"
                Me.Restaurar()
            End If

        Else
            Me.Restaurar()
        End If

    End Sub

#Region "eventos picture"

    Private Sub Picture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBoxBorrar.Click, PictureBoxCancelar.Click, PictureBoxGuardar.Click

        Dim auxPicture As PictureBox = CType(sender, PictureBox)

        Select Case auxPicture.Name
            Case PictureBoxCancelar.Name
                Me.Close()
            Case PictureBoxBorrar.Name
                Borrar()
            Case PictureBoxGuardar.Name
                Me.Guardar()
        End Select

    End Sub

    Private Sub Picture_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBoxBorrar.MouseHover, PictureBoxCancelar.MouseHover, PictureBoxGuardar.MouseHover

        Dim auxPicture As PictureBox = CType(sender, PictureBox)

        Select Case auxPicture.Name

            Case PictureBoxBorrar.Name
                PictureBoxBorrar.BackgroundImage = Image.FromFile("Img/Botones/BBorrarCon.png")
            Case PictureBoxGuardar.Name
                PictureBoxGuardar.BackgroundImage = Image.FromFile("Img/Botones/BGuardarCon.png")
            Case PictureBoxCancelar.Name
                PictureBoxCancelar.BackgroundImage = Image.FromFile("Img/Botones/BCancelarCon.png")

        End Select

    End Sub

    Private Sub Picture_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBoxBorrar.MouseLeave, PictureBoxCancelar.MouseLeave, PictureBoxGuardar.MouseLeave

        Dim auxPicture As PictureBox = CType(sender, PictureBox)

        Select Case auxPicture.Name
            Case PictureBoxBorrar.Name
                PictureBoxBorrar.BackgroundImage = Image.FromFile("Img/Botones/BBorrarSin.png")
            Case PictureBoxGuardar.Name
                PictureBoxGuardar.BackgroundImage = Image.FromFile("Img/Botones/BGuardarSin.png")
            Case PictureBoxCancelar.Name
                PictureBoxCancelar.BackgroundImage = Image.FromFile("Img/Botones/BCancelarSin.png")

        End Select

    End Sub

#End Region




End Class